package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.dababases.dao.OrderDetailDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.OrderDetail;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 订单详情Pane
 * @date 2019-06-21 16:49
 * @email suiyu_yang@163.com
 */
public class OrderDetailPane extends AnchorPane implements Initializable {
    private Orders orders;
    private Stage stage;
    private Buyer buyer;
    private List<OrderDetail> orderDetailsList;
    private int itemsPerPage = 3;

    @FXML
    private Pagination pagination;

    @FXML
    private VBox itemsListVBox;

    @FXML
    private Label sumPriceOfAllItemLabel;

    @FXML
    private Label address;

    @FXML
    private Label ordersIdLabel;

    public OrderDetailPane(Orders orders) {
        this.orders = orders;
        this.orderDetailsList = new OrderDetailDao().getOrderDetailByOrderId(orders.getId());
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/OrderDetailPane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        updateOtherUI();
        updatePages();
    }

    public void showPane() {
        stage = new Stage();
        stage.setScene(new Scene(this));
        stage.show();
    }


    private void updateOtherUI() {
        address.setText(orders.getAddress());
        sumPriceOfAllItemLabel.setText("¥" + orders.getPaymentPrice());
        ordersIdLabel.setText(orders.getId() + "");
    }

    /**
     * 更新页面
     */
    private void updatePages() {
        itemsListVBox.getChildren().clear();

        int pageNum;
        if (orderDetailsList.size() % itemsPerPage == 0) {
            pageNum = orderDetailsList.size() / itemsPerPage;
        } else {
            pageNum = orderDetailsList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        if (orderDetailsList.size() != 0) {
//            itemsListVBox.getChildren().clear();
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < orderDetailsList.size()) {
                    OrderDetailHBox orderDetailHBox = new OrderDetailHBox(orderDetailsList.get(i));
                    itemsListVBox.getChildren().add(orderDetailHBox);
                }
            }
        }
        return itemsListVBox;
    }

}
